<?php

namespace App\Http\Controllers\Api;

use App\Models\{CarActivity, CarProperty, GoodsCat, GoodsCommon};
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

class ActivitiesController extends Controller
{
    public function show(CarActivity $carActivity)
    {
        $data = $carActivity->only(
            'car_activity_id', 'car_activity_name', 'car_activity_time', 'car_activity_show', 'car_activity_order',
            'car_activity_image', 'car_cat_id', 'goods_common_id', 'activity_recommend_image', 'activity_recommend_url'
        );
        $data['goods_common_id'] = $carActivity->goods_common_id ?? [];

        $data['cat_name'] = $carActivity->goodsCat->cat_name;
        $data['brand'] = $carActivity->goodsCat->brands();
        $data['goods_info'] = GoodsCommon::findByIds($data['goods_common_id'])->toArray();
        $data['goods_list'] = GoodsCommon::getActivityGoods($carActivity)->items();

        return $this->success($data);
    }

    /**
     * 专题品牌列表
     *
     * @param CarActivity $carActivity
     * @return Response
     */
    public function activityBrands(CarActivity $carActivity)
    {
        return $this->success(['brand' => $carActivity->goodsCat->brands()]);
    }

    /**
     * 专题商品列表
     *
     * @param Request $request
     * @param CarActivity $carActivity
     * @return Response
     */
    public function activityGoods(Request $request, CarActivity $carActivity)
    {
        $activityGoods = GoodsCommon::getActivityGoods($carActivity, $request->all());

        return $this->success(['goods_list' => $activityGoods->items()]);
    }

    public function sortFields(Request $request)
    {
        $this->validate($request, [
            'cat_id' => 'required|exists:yf_goods_cat,cat_id'
        ], [
            'cat_id.required' => '分类 id 不能为空',
            'cat_id.exists' => '分类 id 不存在'
        ]);

        $goodsCat = GoodsCat::find($request->cat_id);

        $properties = CarProperty::getPropertyAndValue($goodsCat);

        $brands = $goodsCat->brands();

        return $this->success(['property' => $properties, 'brand' => $brands]);
    }
}
